import { nextTick } from 'vue'

// 修改树级样式
export const setTreeStyle = (uuid) => {
    nextTick(() => {
        const tableEl = document.getElementById(`table${uuid}`)
        if(!tableEl) return
        const el = tableEl.getElementsByTagName("tbody")[0];
        const tableRow = el.querySelectorAll(".ant-table-row");
        tableRow.forEach((item) => {
            item.classList.forEach((v, index) => {
                if (v.indexOf("ant-table-row-level-") != -1) {
                    let leaf = v.split("-");
                    leaf = leaf[leaf.length - 1] * 1;
                    if (leaf) {
                        const antTableCell = item.querySelectorAll(".ant-table-cell");
                        for (let i = 0; i < antTableCell.length; i++) {
                            const j = antTableCell[i];
                            if(j.classList[0]=='ant-table-cell' && j.classList[1]=='ant-table-cell-ellipsis'){
                                j.style.paddingLeft = (leaf + 1) * 16 + "px";
                                break;
                            }
                        }
                    }
                }
            });
        });
    });
};

/**
 * 查找交换数据的所有同级
 * @param {*} treeData 树结构数据 
 * @param {*} rowId 当前行的rowKey值
 * @param {*} rowKey rowKey 唯一字段参数名
 * @returns 
 * */ 
export const getBrotherNodes = (treeDataList, pid)=> {  
    if(treeDataList[0].pid == pid) {
        return treeDataList
    }

    for (let i = 0; i < treeDataList.length; i++) {
        let item = treeDataList[i]
        if (item.children) {
            let list = getBrotherNodes(item.children,pid)
            if(list != null) {
                return list
            }
        }
        
    }
}


// 添加层级
export const transverseNode = (nodes, level = 1, cb, parentNode, childKey = "children") => {
    nodes.forEach((node) => {
        if (node[childKey] && node[childKey].length > 0) {
            transverseNode(node[childKey], level + 1, cb, node, childKey);
        }
        cb(node, level, parentNode);
    });
    return nodes;
}

//树形结构铺平
export const getFlatNode = (nodes, flatList, childrenKey = "children") => {
    nodes.forEach((node) => {
        flatList.push(node);
        if (node[childrenKey] && node[childrenKey].length) {
            getFlatNode(node[childrenKey], flatList);
        }
    });
}

//获取元素所在数据的下标
export const getElIndex = (el, arr,idname) => {
    const id = el.getAttribute('data-row-key')
    return arr.findIndex(item => 
        item[idname] == id
    )
}

// 重组成树
export const getTreeData = (nodes, resultList) => {
    const childStack = [];
    let lastNode = {};
    nodes.forEach((node) => {
        delete node.children;
        const pushNode = (dataNode) => {
            const parentNode = childStack[childStack.length - 1];
            if (parentNode) {
                parentNode.children.push(dataNode);
            } else {
                resultList.push(dataNode);
            }
        };
        if (node.level < lastNode.level) {
            childStack.length = node.level - 1;
            pushNode(node);
        } else if (node.level === lastNode.level) {
            pushNode(node);
        } else if (node.level > lastNode.level) {
            if (!lastNode.children) {
                lastNode.children = [];
            }
            childStack.push(lastNode);
            pushNode(node);
        } else {
            resultList.push(node);
        }
        lastNode = node;
    });
    return resultList;
}

export const deleteEl = (el) => {
    nextTick(() => {
        const trList = el.querySelectorAll("tr");
        trList.forEach((item) => {
            item.getAttribute("draggable");
            if (item.getAttribute("draggable") === 'false') {
                item.remove()
            }
        });
    });
}